﻿using UnityEngine;
using System.Collections;

public class BuildingBatch : MonoBehaviour {

    public Mesh boxMesh;
    public Mesh tunnelMesh;
    public Mesh roofMesh;
    public Mesh planRoofMesh;
    public Mesh wallMesh;
    public GameObject meshTemplate;
    public GameObject wallTemplate;

	// Use this for initialization
	void Start () {
	}

    [ButtonCallFunc()]
    public bool GenNow;
    public void GenNowMethod()
    {
        GenOneBuilding();
    }

    void GenOneBuilding()
    {
        //生成地板Box
        var box = GameObject.Instantiate<GameObject>(meshTemplate);
        box.GetComponent<MeshFilter>().mesh = boxMesh;
        box.transform.parent = this.transform;
        Utils.InitGameObject(box);
        box.transform.localPosition = new Vector3(0, 1, 0);
        box.transform.localScale = new Vector3(8, 1, 6);


        var layer1 = new GameObject();
        layer1.transform.parent = transform;
        Utils.InitGameObject(layer1);
        layer1.transform.localPosition = new Vector3(0, 11, 0);

        var initPos = -9.0f;
        for(var i = 0; i < 10; i++)
        {
            var b1 = GameObject.Instantiate<GameObject>(meshTemplate);
            b1.GetComponent<MeshFilter>().mesh = tunnelMesh;
            b1.transform.parent = layer1.transform;
            Utils.InitGameObject(b1);
            b1.transform.localRotation = Quaternion.Euler(0, 90, 0);
            b1.transform.localPosition = new Vector3(initPos+i*2, 0, 7);
        }

        for(var i = 0; i < 10; i++)
        {
            var b1 = GameObject.Instantiate<GameObject>(meshTemplate);
            b1.GetComponent<MeshFilter>().mesh = tunnelMesh;
            b1.transform.parent = layer1.transform;
            Utils.InitGameObject(b1);
            b1.transform.localRotation = Quaternion.Euler(0, 90, 0);
            b1.transform.localPosition = new Vector3(initPos+i*2, 0, -7);
        }

        var initZ = -7.0f;
        for(var i = 0; i < 8; i++)
        {
            var b1 = GameObject.Instantiate<GameObject>(meshTemplate);
            b1.GetComponent<MeshFilter>().mesh = tunnelMesh;
            b1.transform.parent = layer1.transform;
            Utils.InitGameObject(b1);
            b1.transform.localRotation = Quaternion.Euler(0, 0, 0);
            b1.transform.localPosition = new Vector3(-9, 0, initZ + i*2);
        }

        for (var i = 0; i < 8; i++)
        {
            var b1 = GameObject.Instantiate<GameObject>(meshTemplate);
            b1.GetComponent<MeshFilter>().mesh = tunnelMesh;
            b1.transform.parent = layer1.transform;
            Utils.InitGameObject(b1);
            b1.transform.localRotation = Quaternion.Euler(0, 0, 0);
            b1.transform.localPosition = new Vector3(9, 0, initZ + i * 2);
        }


        var wallLayer = new GameObject();
        wallLayer.transform.parent = transform;
        Utils.InitGameObject(wallLayer);
        wallLayer.transform.localPosition = new Vector3(0, 6, 0);

        var winitX = -7;
        var xnum = 8;
        var winitZ = -5;
        var znum = 6;
        //WallFace
        for(var i = 0; i < xnum; i++)
        {
            var b1 = GameObject.Instantiate<GameObject>(wallTemplate);
            b1.GetComponent<MeshFilter>().mesh = wallMesh;
            b1.transform.parent = wallLayer.transform;
            Utils.InitGameObject(b1);

            b1.transform.localPosition = new Vector3(winitX+i*2, 0, 6);
            b1.transform.localRotation = Quaternion.Euler(0, 180, 0);
            b1.transform.localScale = new Vector3(1, 4, 1);
        }

        for(var i = 0; i < xnum; i++)
        {
            var b1 = GameObject.Instantiate<GameObject>(wallTemplate);
            b1.GetComponent<MeshFilter>().mesh = wallMesh;
            b1.transform.parent = wallLayer.transform;
            Utils.InitGameObject(b1);

            b1.transform.localPosition = new Vector3(winitX+i*2, 0, -6);
            b1.transform.localRotation = Quaternion.Euler(0, 0, 0);
            b1.transform.localScale = new Vector3(1, 4, 1);
        }

        for(var i = 0; i < znum; i++)
        {
            var b1 = GameObject.Instantiate<GameObject>(wallTemplate);
            b1.GetComponent<MeshFilter>().mesh = wallMesh;
            b1.transform.parent = wallLayer.transform;
            Utils.InitGameObject(b1);

            b1.transform.localPosition = new Vector3(-8, 0, winitZ+i*2);
            b1.transform.localRotation = Quaternion.Euler(0, 90, 0);
            b1.transform.localScale = new Vector3(1, 4, 1);
        }

        for(var i = 0; i < znum; i++)
        {
            var b1 = GameObject.Instantiate<GameObject>(wallTemplate);
            b1.GetComponent<MeshFilter>().mesh = wallMesh;
            b1.transform.parent = wallLayer.transform;
            Utils.InitGameObject(b1);

            b1.transform.localPosition = new Vector3(8, 0, winitZ+i*2);
            b1.transform.localRotation = Quaternion.Euler(0, -90, 0);
            b1.transform.localScale = new Vector3(1, 4, 1);
        }

    }


}
